package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.markt.model.admin.Club;
import com.ls.markt.model.admin.Events;
import com.ls.markt.model.admin.Player;
import com.ls.markt.service.admin.ClubService;
import com.ls.markt.service.admin.EventsService;
import com.ls.markt.service.admin.PlayerService;
import com.ls.markt.util.WebResult;
import com.ls.markt.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author ls
 * 俱乐部的控制器类
 */
@Controller
@RequestMapping("/club")
public class ClubController {

    @Autowired
    private ClubService clubService;

    @Autowired
    private PlayerService playerService;

//    @Resource
//    private EnumManageItemService enumManageItemService;

    @Resource
    EventsService eventsService;

    @GetMapping("/index")
    public String index(){
        return "admin/club/index";
    }

//    @RequestMapping("/list")
//    @ResponseBody
//    public JSONObject getCluds(@RequestParam(value="page",required=false,defaultValue="1")int page,
//                               @RequestParam(value="rows",required=false,defaultValue="10")int rows){
//        PageRequest pageRequest = PageRequest.of(page-1,rows);
//        Page<Club> clubs= clubService.listClubs(pageRequest);
//        JSONArray jsonArray = new JSONArray();
//        if (clubs.getSize()>0){
//            for (Club club:clubs){
//                JSONObject clubJson = (JSONObject)JSONObject.toJSON(club);
//                Map<String,Object> map = playerService.listInfoByClub(club.getId());
//                clubJson.put("squad",map.get("count"));
//                clubJson.put("age",map.get("age"));
//                clubJson.put("value",map.get("value"));
//                clubJson.put("totalValue",map.get("totalvalue"));
//                jsonArray.add(clubJson);
//            }
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("total",clubs.getTotalElements());
//        jsonObject.put("rows",jsonArray);
//        return jsonObject;
//    }

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject getCluds(@RequestParam(value="page",required=false,defaultValue="1")int page,
                               @RequestParam(value="rows",required=false,defaultValue="10")int rows){
        PageRequest pageRequest = PageRequest.of(page-1,rows);
        Page<Club> clubs= clubService.listClubs(pageRequest);
        JSONArray jsonArray = new JSONArray();
        if (clubs.getSize()>0){
            for (Club club:clubs){
                JSONObject clubJson = (JSONObject)JSONObject.toJSON(club);
                String name = eventsService.getNameById(club.getLevel());
                clubJson.put("levelName",name);
                jsonArray.add(clubJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",clubs.getTotalElements());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return "admin/club/add";
    }

    /**
     * 跳转到俱乐部详情页
     * @return
     */
    @GetMapping("/detail/{clubId}")
    public ModelAndView detail(@PathVariable("clubId")Long clubId,Model model){
        model.addAttribute("clubId",clubId);
        return new ModelAndView("admin/club/detail","detailModel",model);
    }

    @PostMapping("/clubDetail/{clubId}")
    @ResponseBody
    public JSONObject detail(@PathVariable("clubId")Long clubId){
        List<Player> players = playerService.listPlayersByClubId(clubId);
        JSONArray jsonArray = new JSONArray();
        if (players.size()>0){
            for (Player player:players){
                JSONObject playerJson = (JSONObject)JSONObject.toJSON(player);
                playerJson.put("clubName",clubService.getClubName(player.getClub()));
                jsonArray.add(playerJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",jsonArray.size());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }


    /**
     * 跳转到编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id, Model model){
        Club club = clubService.getClubById(id);
        model.addAttribute("club",club);
        return new ModelAndView("admin/club/edit","clubModel",model);
    }

    @PostMapping("/getEvents")
    @ResponseBody
    public JSONArray getEvents(){
        JSONArray jsonTree = new JSONArray();
        List<Events> eventsList = eventsService.findAll();
        for (Events events: eventsList) {
            JSONObject jNode = new JSONObject();
            jNode.put("id",events.getId());
            jNode.put("text",events.getName());
            jsonTree.add(jNode);
        }
        return jsonTree;
    }

    /**
     * 保存，更新数据
     * @param club
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(Club club){
        clubService.saveClub(club);
        return new Response(true);
    }

    /**
     * 执行删除俱乐部
     * @param ids
     * @return
     */
    @PostMapping("/doDelete")
    @ResponseBody
    public Response doDelete(@RequestParam Long[] ids){
        clubService.removeClub(ids);
        return new Response(true);
    }

    @RequestMapping("/getClubTree")
    @ResponseBody
    public JSONArray getCludTree(){
        JSONArray jsonTree = new JSONArray();
        List<Club> clubs = clubService.listClubs();
        for (Club club:clubs){
            JSONObject jNode = new JSONObject();
            jNode.put("id",club.getId());
            jNode.put("text",club.getName());
            jNode.put("value",club.getId());
            jsonTree.add(jNode);
        }
        return jsonTree;
    }

    @PostMapping("/import")
    @ResponseBody
    public JSONObject importExcel(@RequestParam("file")MultipartFile file) throws IOException {
        clubService.importExcel(file);
        return WebResult.success();
    }
}
